<?php
	#phpinfo();
	#$conn = mysql_connect('localhost','root','qazokm2?');//括号的里的分别代表主机、用户名、密码
	$conn = mysql_connect('192.168.1.100:3307','elincredit','123456');//括号的里的分别代表主机、用户名、密码
	mysql_select_db('iguoyi_crawler',$conn);//选择库
	mysql_query('set names utf8',$conn);//设置字符集
	#print_r($conn);exit;


	//**************获取一幅画的详细信息 开始**************************************
	//$url = 'http://auction.artron.net/showpic.php?picid=art01110276';
	//get_one_info($url,$conn,11,1,'2000.0');//获取一副画作详细信息
	//exit;
	//**************获取一幅画的详细信息 结束**************************************
	$sql_all_name = 'select id,name from iguoyi_artist where id in(182) order by id';
	#$sql_all_name = 'select id,name from iguoyi_artist';
	$result = mysql_query($sql_all_name,$conn);
	#print_r($result);exit;
	#if($name_array){
	#$row = mysql_fetch_array($result,MYSQL_ASSOC);
	#print_r($row['name']);
	#die;
        #file_put_contents('a.txt',$row['name']);	
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
		$name_arr[$row['id']] = trim($row['name']);
	}
	#}
#	$name_arr = array(
#				#'范曾',
#				#'刘大为',
#				#'何家英',
#				#'田黎明',
#				#'尹沧海',
#				#'李老十',
#				#'袁武',
#				'黄胄',
#				#'李津',
#				#'霍春阳'
#			);
#	print_r($name_arr);
#	die;
	if($name_arr){
		foreach($name_arr as $k => $user_name){
			//echo $user_name;
			$user_id = $k;
			#$user_id = $k+11;
			#$user_id = $k+8;

			#echo $user_name;
			$info = $user_name.'--'.date('Y-m-d H:i:s');
			echo $info;
			file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);

			$url = 'http://amma.artron.net/artronindex_pic.php?artist='.$user_name;
			#echo $url."\n";
			$content = file_get_contents($url);
			//****分解html 开始****************/
			$c_arr = explode('<tbody>',$content);
			$c_arr = explode('</tbody>',$c_arr[count($c_arr)-1]);
			$c_arr = explode('</tr>',$c_arr[0]);
			array_pop($c_arr);
			array_pop($c_arr);
			#print_r($c_arr[count($c_arr)-1]);
			if($c_arr){
				foreach($c_arr as $c_str){
				        #echo $c_str;exit;	
					$param = html_str($c_str); 
					#print_r($param);
					if($param){
						//print_r($param);exit;	
						$href = array_pop($param);
						//print_r($href);exit;
						$sql_s = 'select count(*) as num from iguoyi_auction where artist_id='.$user_id;
						$result = mysql_query($sql_s,$conn);
						#$row = mysql_fetch_array($result);	
						//print_r($row);exit;
						//if(!$row['num']){ //记录唯一性判断
							$auction_insert_id = auction_insert($param,$conn,$user_id); 	
							//echo $auction_insert_id;exit;
							if($auction_insert_id){ //拍卖季
								get_q_list($href,$user_id,$conn,$auction_insert_id,$param['name_labe']); //拍卖季列表
								//exit;
							}else{
								$info = 'error:';
								echo $info;
								file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
							}
						//}
					}
				}
			}
			//****分解html 结束****************/
			#file_put_contents('a.txt',$content);
			//exit();
		}
	}

	//拍卖季数据入库
	function auction_insert($param,$conn,$user_id){
		$sql_in = "insert into iguoyi_auction(`artist_id`,`name`,`name_labe`,`auction_count`,
					`tran_count`,`tran_money`,`tran_rate`,`average_price`)
					values($user_id,'$param[name]','$param[name_labe]','$param[auction_count]',
					'$param[tran_count]', '$param[tran_money]','$param[tran_rate]',
					'$param[average_price]')";
		//echo $sql_in;//exit;
	        if(mysql_query($sql_in,$conn)){
			$return = mysql_insert_id();
		}else{
			$return = 0;
		}	
		return $return;
	     	
	} 	

	//处理单个拍卖季html信息
	function html_str($str){ 
		$tmp = explode('<td>',$str);
		array_shift($tmp);
		if($tmp){
			$tmp = str_replace('</td>','',$tmp);

			$name = $name_labe = trim($tmp[0]);
		//	if(intval($name)>=2010){
				if(strpos($name_labe,'春季')){
					$name_labe = str_replace('春季','.0',$name_labe);	
				}elseif(strpos($name_labe,'秋季')){
					$name_labe = str_replace('秋季','.1',$name_labe);	
				}

				$auction_count = trim($tmp[1]);
				$tran_count = trim($tmp[2]);
				$tran_money = trim($tmp[3]);
				$tran_rate = trim($tmp[4]);
				$average_price = trim($tmp[5]);
				$href = explode('href="',$tmp[6]);
				$href = explode('"',$href[count($href)-1]);
				$href = 'http://amma.artron.net/'.trim($href[0]);

				$param = array(
					'name' => $name, //拍卖季名称		
					'name_labe' => $name_labe, //拍卖季标签
					'auction_count' => $auction_count, //上拍数量
					'tran_count' => $tran_count, //成交数量		
					'tran_money' => $tran_money, //成交金额		
					'tran_rate' => $tran_rate, //成交率	
					'average_price' => $average_price, //均价（元/平方尺）
					'href' => $href
				);
			#	if(intval($name)==2001){
			#		print_r($param);
			#	}
				return  $param;
		//	}else{
		//		return  0;
		//	}
		}

	}
	//获取该画家拍卖季作品列表并入库 17/01/22 shenyunsheng@elincreditl.com
	function get_q_list($url,$user_id,$conn,$auction_id,$auction_labe){
		//echo $url;exit;
		$c = file_get_contents($url);
		//print_r($c);exit;
		if($c){
			$c = explode('<tbody>',$c);
			$c = explode('</tbody>',$c[count($c)-1]);
			$c = explode('</tr>',$c[0]);
			array_pop($c);
			//print_r($c);exit;
			if($c){
				foreach ($c as $one_info){
					//echo $one_info;exit;
					$tmp = explode('</td>',$one_info);
					array_shift($tmp);
					array_pop($tmp);
					$tmp = str_replace('<td align="center">','',$tmp);
					//print_r($tmp);exit;
					if($tmp){
						$t_info = trim($tmp[0]);
						$t_info = explode('href= "',$t_info);
						$t_info = trim($t_info[count($t_info)-1]);
						$t_info = str_replace('</a>','',$t_info);
						$t_info = explode('">',$t_info);
						$painting_name = explode(' ',trim($t_info[1]));
						array_shift($painting_name);
						$painting_name = join(' ',$painting_name);
						$param = array(
							'artist_id' => $user_id,
							'auction_id' => $auction_id, //拍卖季id
							'auction_labe' => $auction_labe, //拍卖季标签
							'one_url' => trim($t_info[0]), //画作抓取url
							'painting_name' => $painting_name, //画作名称
							'evaluate_price' => trim($tmp[1]), //估价
							'tran_price' => trim($tmp[2]), //成交价
							'auction_date' => trim($tmp[3]), //拍卖日期 
							'auction_room' => trim($tmp[4]), //拍卖机构 
						);
						//print_r($param);exit;
						if($param){
							//唯一性查询
							$sql_painting = "select count(*) as num from iguoyi_painting
									where artist_id=".$param['artist_id'].
									" and name='".$param['painting_name']."'";
							//echo $sql_painting;exit;
							$result = mysql_query($sql_painting,$conn);
							#$row = mysql_fetch_array($result);
							//print_r($row);exit;
							//if(!$row['num']){
								//echo $sql_painting;exit;
								//画作信息入库
								$sql_painting_in = "insert into iguoyi_painting(`artist_id`,`name`,`url`)values($param[artist_id],'$param[painting_name]','$param[one_url]')";
								//echo $sql_painting_in;exit;
								if(mysql_query($sql_painting_in,$conn)){
									$painting_id = mysql_insert_id();
									//画作与画家关系入库
									$sql_artist_painting_rel = "insert into iguoyi_artist_painting_relation (`artist_id`,`painting_id`)values($param[artist_id],$painting_id)";
									//echo $sql_artist_painting_rel;exit;
									if(mysql_query($sql_artist_painting_rel,$conn)){
										$info = '画作与画家关系入库成功！';
										echo $info;
										file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
									}else{
										$info = '画作与画家关系入库失败!';
										echo $info;
										file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
									}
									//画作与拍卖关系入库
									$sql_painting_auction_relation = "insert into iguoyi_painting_auction_relation(`artist_id`,`painting_id`,`auction_id`,
`evaluate_price`,`tran_price`,`auction_room`,`auction_date`)values('$param[artist_id]',
$painting_id,'$param[auction_id]','$param[evaluate_price]','$param[tran_price]',
'$param[auction_room]','$param[auction_date]')";
									//echo $sql_painting_auction_relation;exit;
									if(mysql_query($sql_painting_auction_relation)){
										$info = '画作与拍卖关系入库成功!';
										echo $info;
										file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
									}else{
										$info = '画作与拍卖关系入库失败!';
										echo $info;
										file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
									}
									get_one_info($param['one_url'],$conn,$painting_id,$param['artist_id'],$param['auction_labe']);//获取一副画作详细信息
								}else{
									$info = '画作入库失败！';
									echo $info;
									file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
								}
						//	}
						}
					}
				}
			}
		}else{
			$info = '列表不存在!';
			echo $info;
			file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
		}
	}
	
	//获取一副画作信息并更新相关信息  17/01/22  shenyunsheng@elincredit.com
	function get_one_info($url,$conn,$painting_id,$artist_id,$auction_labe){
		#echo $url;
		echo $auction_labe.'/'.$artist_id.'/';

		//header('Content-type:text/html;charset=utf-8');
		$path = '/data/www/works_182/'.$auction_labe.'/'.$artist_id.'/';
		//echo $path;exit;
		if(!is_dir($path)){ //判断路径是否存在,如果不存在，则创建。
			mkdir($path,0777,true);
		}
		
		//*****获取远程一副画作页面html 开始*************
		#$one_info = file_get_contents($url);
		$opts = array(
			'http'=>array(
				'method'=>"GET",
				'timeout'=>60,
			)
		);
		$context = stream_context_create($opts);
		$cnt=0;
		while($cnt < 3 && ($one_info=@file_get_contents($url,false,$context))===FALSE){ sleep(5); $cnt++;};	
		#print_r($one_info);exit;
		//*****获取远程一副画作页面html 结束*************
		//处理开始
		if(strpos($one_info,'如果您的浏览器没有跳转') !== false){
			
			$info = 'sorry,画作不存在!!!';
			echo $info;
			file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
			return 0;
		}else{
			$one_info = explode('<div class="worksInfo">',$one_info);
			$img_info = $one_info[0];
			$one_info = explode('</div>',$one_info[count($one_info)-1]);
			$one_info = $one_info[0];
			$one_info = explode('</tr>',$one_info);
			array_pop($one_info);

			//作品尺寸
			$works_size = $one_info[0];	
			$works_size = explode('<em>',$works_size);
			$works_size = explode('</em>',$works_size[count($works_size)-1] );
			$works_size = $works_size[0];	
			//print_r($works_size);
			//作品分类/创作年代
			$works_class = explode('</td>',$one_info[1]);	
			array_pop($works_class);
			$works_age = explode('<td class="col">',$works_class[1]);	
			$works_age = $works_age[count($works_age)-1]; 
			$works_class = explode('<td class="col">', $works_class[0]);	
			$works_class = $works_class[count($works_class)-1]; 
			//print_r($works_age);
			//专场
			$works_special = explode('</td>',$one_info[4]);	
			$works_special = explode('target="_blank">',$works_special[0]);	
			$works_special = str_replace('</a>','',$works_special[count($works_special)-1]);
			//print_r($works_special);
			//拍卖公司/拍卖会
			$works_company = explode('</td>',$one_info[5]);	//公司
			array_pop($works_company);
			$works_meeting = $works_company[count($works_company)-1];
			$works_company = $works_company[0];
			$works_company = explode('</font>',$works_company);
			$works_company = explode('<font class="cdRed">',$works_company[0]);
			$works_company = $works_company[count($works_company)-1];
			$works_meeting = explode('target="_blank">',$works_meeting); 
			$works_meeting = str_replace('</a>','',$works_meeting[count($works_meeting)-1]);
			//print_r($works_meeting);
			//画作简介
			$works_desc = explode('</td>',$one_info[count($one_info)-1]);	
			$works_desc = explode('colspan="3">',$works_desc[0]);	
			$works_desc = $works_desc[count($works_desc)-1];	
			//print_r($works_desc);
			//exit;

			$img_info = explode('gotobigpic',$img_info);
			$img_info = explode('shareBigPics="',$img_info[0]);
			$img_info = explode('";',$img_info[count($img_info)-1]);
			$img_info = $img_info[0];

			$img_name = explode('/',$img_info);
			$img_name = array_pop($img_name);
			//echo $img_name;exit;
			//*******读取远程图片 开始**************
			//echo $img_info;exit;
			//$works = file_get_contents('http://crs.elincredit.com/statics/images/advertise/index1_logo.png');
			#$works = file_get_contents($img_info);	
			$works = get_remote_img($img_info);
			if(!$works){
				sleep(5);
				#http://img5.artron.net/auction/old/art1162/d/art11620060.jpg	
				$url_tmp = explode('.',$img_info);
				array_shift($url_tmp);
				$img_info = $url_tmp = 'http://img1.'.join('.',$url_tmp);
				#echo $url_tmp;exit;
				$works = get_remote_img($url_tmp);
			}

			if(!$works){
				sleep(5);
				#http://img5.artron.net/auction/old/art1162/d/art11620060.jpg	
				$url_tmp = explode('.',$img_info);
				array_shift($url_tmp);
				$img_info = $url_tmp = 'http://img2.'.join('.',$url_tmp);
				$works = get_remote_img($url_tmp);

			}

			if(!$works){
				sleep(5);
				#http://img5.artron.net/auction/old/art1162/d/art11620060.jpg	
				$url_tmp = explode('.',$img_info);
				array_shift($url_tmp);
				$img_info = $url_tmp = 'http://img3.'.join('.',$url_tmp);
				$works = get_remote_img($url_tmp);
			}

			if(!$works){
				sleep(5);
				#http://img5.artron.net/auction/old/art1162/d/art11620060.jpg	
				$url_tmp = explode('.',$img_info);
				array_shift($url_tmp);
				$img_info = $url_tmp = 'http://img4.'.join('.',$url_tmp);
				$works = get_remote_img($url_tmp);

			}

			if(!$works){
				sleep(5);
				#http://img5.artron.net/auction/old/art1162/d/art11620060.jpg	
				$url_tmp = explode('.',$img_info);
				array_shift($url_tmp);
				$img_info = $url_tmp = 'http://img5.'.join('.',$url_tmp);
				$works = get_remote_img($url_tmp);

			}

			if(!$works){

				sleep(5);
				#http://img5.artron.net/auction/old/art1162/d/art11620060.jpg	
				$url_tmp = explode('.',$img_info);
				array_shift($url_tmp);
				$img_info = $url_tmp = 'http://img6.'.join('.',$url_tmp);
				$works = get_remote_img($url_tmp);
			}
			
			if(!$works){
				sleep(5);
				#http://img5.artron.net/auction/old/art1162/d/art11620060.jpg	
				$url_tmp = explode('.',$img_info);
				array_shift($url_tmp);
				$img_info = $url_tmp = 'http://img7.'.join('.',$url_tmp);
				$works = get_remote_img($url_tmp);
			}

			$flag = 0;
			if($works){
				//保存图片
				$file_name = $path.$img_name;
				$flag = file_put_contents($file_name,$works); 	
			}else{
				$info = $img_info;
				echo $info;
				file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
			}
			#echo $flag;exit;
			//*******读取远程图片 结束**************
			if($flag){ //图片抓取成功，保存到本地。下面保存其他相关信息。
				//拍品信息处理
				$param = array(
					'artist_id' => $artist_id, //画家id
					'painting_id' => $painting_id, //画作id
					'img_url'  => $img_info, //画作远程url 
					'img_origin_path' => trim('/'.$auction_labe.'/'.$artist_id.'/'.$img_name), //文件保存路径
					'works_size'  => trim($works_size), //作品尺寸
					'works_class' => trim($works_class), //作品分类
					'works_age'   => trim($works_age),  //作品年代
					'works_special' => trim($works_special), //专场
					'works_company'  => trim($works_company), //拍卖公司
					'works_meeting'  => trim($works_meeting), //拍卖会
					'works_desc' => trim($works_desc) //画作简介
				);
				//画作信息更新
				$sql_p_up = "update iguoyi_painting set 
						 `img_url`='$param[img_url]',
						 `size`='$param[works_size]',
						 `class`='$param[works_class]',
						 `creation_year`='$param[works_age]',
						 `desc`='$param[works_desc]',
						 `img_origin_path`='$param[img_origin_path]'
						 where id=".$param['painting_id'];
				//echo $sql_p_up;exit;
				if(mysql_query($sql_p_up,$conn)){
					$info = '画作信息更新成功!';
					echo $info;
					file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
				}else{
					$info = '画作信息更新失败！';
					echo $info;
					file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
				}
				//echo $sql_p_up;exit;

				//画作与拍卖信息关系信息更新
				$sql_auction_up = "update iguoyi_painting_auction_relation
							set `auction_session`='$param[works_special]',
								`auction_name` = '$param[works_meeting]',
								`auction_room` = '$param[works_company]'
							where painting_id=".$param['painting_id'];
				if(mysql_query($sql_auction_up,$conn)){
					$info = '画作与拍卖信息关系更新成功!';
					echo $info;
					file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
				}else{
					$info = '画作与拍卖信息关系更新失败！';
					echo $info;
					file_put_contents('igoyi_crawler.log',$info,FILE_APPEND);
				}
			       //echo $sql_auction_up;exit;
			}
		}
	}


	//curl 获取远程图片
	function get_remote_img($img_info){
		$ch = curl_init();
		//设置选项，包括URL
		curl_setopt($ch, CURLOPT_URL, $img_info);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		//执行并获取HTML文档内容
		$works = curl_exec($ch);
		//释放curl句柄
		curl_close($ch);
		return $works;

	}
?>
